// pages/friends/index.ts
import tran from '../../../utils/language'
import { friendRequestList, userTargetInfo, presenceList } from '../../../utils/api.js'
import { handleImUserData, debounce } from '../../../utils/index.js'
// 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('addFriendList'),
    _tBase: tran._t('base'),
    token: getApp().globalData.token,
    showEventPopup: false,
    userData: [{
      userId: '324242',
      nickname: 'Benjamin',
      personalityAvatar: '',
      avatarurl: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      status: 1, // 1在线 2忙碌 3离线
      xmove: 0
    }],
    allUserData: [{
      userId: '324242',
      nickname: 'Benjamin',
      personalityAvatar: '',
      avatarurl: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
      status: 1, // 1在线 2忙碌 3离线
      xmove: 0
    }],
    keyword: '',
    downPopupParams: {
      showDownPopup: false,
      showType: 1 // 0 注册 1 今天想做什么
    },
    homeFriendInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      userData: [],
      allUserData: []
    })
    // this.getList()
    this.getFriendRequestList()
  },
  onClose () {
    const {downPopupParams} = this.data
    downPopupParams.showDownPopup = false
    this.setData({
      downPopupParams
    })
    this.getFriendRequestList()
  },
  // 通过我们这边的接口获取用户属性
  getUserStatus (arr: any) {
    const params = {
      userIds: arr || []
    }
    presenceList(params).then((res: any) => {
      res.length > 0 && res.map((item: any) => {
          item.avatarurl = item.avatar
          item.puzzleUrl = item.personalityAvatar
          item.nickname = item.pushName
      })
      this.setData({
        userData: res,
        allUserData: JSON.parse(JSON.stringify(res))
      })
    })
  },
  agreeAndRefuse (e: any) {
    const { downPopupParams } = this.data
    downPopupParams.showDownPopup = false
    this.setData({
      downPopupParams
    })
    // this.getList()
    this.getFriendRequestList()
  },
  
  /**
   * 获取好友请求数量
   */
  getFriendRequestList() {
    const params = {}
    // this.setData({
    //   loading: true
    // })
    this.setData({
      loading: false,
    })
    friendRequestList(params).then((res: any) => {
      // 调用环信的api获取用户属性
      // this.getuserAttribute(res)
      if (res.length > 0) {
        // 调用我们自己的接口获取用户属性
        this.getUserStatus(res)
      } else {
        this.setData({
          userData: [],
          allUserData: []
        })
      }
    })
  },
  /**
   * 防抖搜索
   */
  keywordChange: debounce(function (e: any) {
    let keyword = e[0].detail
    if (keyword) {
      this.setData({
        keyword: keyword
      }, () => {
        this.keyworkSearch()
      })
    } else {
      this.setData({
        userData: this.data.allUserData
      })
    }
  }, 300),
  keyworkSearch() {
    const { keyword, allUserData } = this.data
    let list: any = []
    allUserData.length > 0 && allUserData.map((item: any) => {
      // 名字搜索，前端模糊查询
      if (item.nickname.indexOf(keyword) != -1) {
        list.push(item)
      }
    })
    this.setData({
      userData: list
    })
  },
  getList() {
    const params = {}
    friendRequestList(params).then((res: any) => {
      this.getuserAttribute(res)
    })
  },
  /**
   * 获取用户属性
   * @param arr 
   */
  getuserAttribute(arr: any) {
    const userList = JSON.parse(JSON.stringify(arr))
    wx.conn.fetchUserInfoById(arr).then((res: any) => {
      const userInfoList = handleImUserData(userList, res.data)
      this.setData({
        userData: userInfoList,
        allUserData: JSON.parse(JSON.stringify(userInfoList))
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        activeKey: "friends"
      })
    }
  },
  closeDownPopup() {
    const downPopup = this.selectComponent('#down-popup');
    const { downPopupParams } = this.data
    downPopupParams.showDownPopup = false
    this.setData({ downPopupParams: downPopupParams })
    downPopup && downPopup.hidePopup()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onClosePopup() {
    // const customPopup = this.selectComponent('#event-popup');
    // customPopup && customPopup.hidePopup()
    this.setData({
      showEventPopup: false
    })
  },
  moreTap(e: any) {
    const { info } = e.currentTarget.dataset
    const params = {
      targetUserId: info.userId
    }
    userTargetInfo(params).then(res => {
      const { downPopupParams } = this.data
      downPopupParams.showDownPopup = true
      this.setData({ downPopupParams: downPopupParams, homeFriendInfo: res, })
    })
  },
  /**
   * 查询用户个人资料
   */
  // goFriendInfo() {
  //   const { tapInfo } = this.data
  //   const params = {
  //     targetUserId: tapInfo.userId
  //   }
  //   userTargetInfo(params).then(res => {
  //     const { downPopupParams } = this.data
  //     downPopupParams.showDownPopup = true
  //     this.setData({ downPopupParams: downPopupParams, homeFriendInfo: res, showEventPopup: false })
  //   })
  // },
  methods: {
  }
})